/**
 * 
 */
package hu.gbalage.owlforms.impl.data;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

import hu.gbalage.owlforms.api.Field;
import hu.gbalage.owlforms.api.Form;
import hu.gbalage.owlforms.api.data.FieldData;
import hu.gbalage.owlforms.api.data.FormData;

/**
 * @author Grill Balazs (balage.g@gmail.com)
 *
 */
public class FormDataImpl implements FormData {

	private final Form form;
	
	private final URI uri;
	
	private final Map<URI, FieldData> datas = new HashMap<URI, FieldData>();
	
	public FormDataImpl(Form form, URI uri){
		this.form = form;
		this.uri =uri;	
	}
	
	public FormDataImpl(Form form, FormData parent){
		this.form = form;
		URI puri = parent.getURI();
		String random = Math.round(Math.random()*1000000)+"";
		URI u = null;
		try {
		    u = new URI(puri.getScheme(),puri.getHost(),puri.getPath(),puri.getFragment()+"_"+form.getURI().getFragment()+"_"+random);
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		this.uri = u;
	}
	
	/* (non-Javadoc)
	 * @see hu.gbalage.owlforms.api.data.FormData#getData(hu.gbalage.owlforms.api.Field)
	 */
	public FieldData getData(Field field) {
		if (!datas.containsKey(field.getURI()))
			datas.put(field.getURI(), new FieldDataImpl(this,field));
		return datas.get(field.getURI());
	}

	/* (non-Javadoc)
	 * @see hu.gbalage.owlforms.api.data.FormData#getForm()
	 */
	public Form getForm() {
		return form;
	}

	/* (non-Javadoc)
	 * @see hu.gbalage.owlforms.api.Entity#getURI()
	 */
	public URI getURI() {
		return uri;
	}

}
